<?php

# PMB Online
# Aplikasi PMB Online untuk Pendaftaran Calon Mahasiswa di PT/PTS Indonesia
# 
# version 1.0
# class th_pmb
# file models/th_pmb.php
# created Feb 5, 2013 8:29:47 AM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Th_pmb extends SI_Model {

    var $data = array();

    public function __construct() {
        parent::__construct();
    }

    public function get() {
        $this->db->order_by('tha', 'desc');
        $que = $this->db->get('tha');
        return $que->result_array();
    }
    
    public function get_tha_aktif(){
        $this->db->where('status',1);        
        $que = $this->db->get('tha');
        
        return $que->row_array();
    }
    
    public function add($data) {
        $rules = array(
            array('field' => 'tha', 'label' => 'Th.PMB', 'rules' => 'required|numeric|exact_length[4]|trim'),
        );

        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;
            return false;
        }

        $data['status'] = 0;
        if ($this->_check_tha($data['tha'])) {
            $this->umsg = 'Data Th.PMB gagal ditambahkan, Data sama dengan sebelumnya';
            return false;
        } else {
            if ($this->db->insert('tha', $data)) {
                $this->umsg = 'Data Th.PMB berhasil ditambahkan';
                return true;
            } else {
                $this->umsg = 'Data Th.PMB gagal ditambahkan';
                return false;
            }
        }
    }

    private function _check_tha($tha) {
        $this->db->where('tha', $tha);
        $que = $this->db->get('tha');

        if ($que->num_rows() > 0)
            return true;
        else
            return false;
    }

    public function set_active($id) {
        $this->db->update('tha', array('status' => 0));

        $this->db->where('id_tha', $id);
        $this->db->update('tha', array('status' => 1));
    }

}

